package LeetCode.month02.day50;

public class No02 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode head = new ListNode((l1.val+l2.val)%10);
        int add = (l1.val+l2.val)/10;
        ListNode next = head;
        l1 = l1.next;
        l2 = l2.next;
        while(l1!=null&&l2!=null){
            int x = l1.val;
            int y = l2.val;
            next.next = new ListNode((x+y+add)%10);
            add = (x+y+add)/10;
            l1 = l1.next;
            l2 = l2.next;
            next = next.next;
        }
        while(l1!=null){
            next.next = new ListNode((l1.val+add)%10);
            add=(l1.val+add)/10;
            l1 = l1.next;
            next = next.next;
        }
        while(l2!=null){
            next.next = new ListNode((l2.val+add)%10);
            add=(l2.val+add)/10;
            l2 = l2.next;
            next = next.next;
        }
        while(add!=0){
            next.next = new ListNode(add);
            add= 0;
        }
        return head;
    }
}
